﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include "dwgpoint.h"
#include "dwgline.h"
#include "dwgcircle.h"
#include <QWheelEvent>
#include <QDebug>
#include <QtMath>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/icons/logo.png"));
    this->setWindowTitle("MyCAD");
    this->setStyleSheet("QMainWindow { background-color: white; }"
                        "QToolBar {background-color: rgb(230,230,220)}");
    m_trans.reset();                		// 先把系统的变换矩阵置为空
    m_trans.translate(0,this->height());       // 再平移坐标原点到窗口左下角
    m_trans.scale(1,-1);                    // 对坐标系的Y轴实施反向变换
    this->setMouseTracking(true);
    ui->centralwidget->setMouseTracking(true);

}

MainWindow::~MainWindow()
{
    delete ui;
}

bool isFirst = true;
QPointF firstpt,lastpt;
QPointF temppt;
QPointF movept;
// 绘制坐标系的函数
void MainWindow::DrawCoordinate(QPainter *painter)
{
    painter->save();
    QPointF opt(10,10),xpt(10,50),ypt(50,10);
    painter->setPen(QPen(Qt::red));
    painter->drawLine(opt,xpt);
    painter->drawLine(opt,ypt);
    painter->restore();

}

void MainWindow::paintEvent(QPaintEvent *event)
{

    QPainter *painter = new QPainter(this);
    painter->save();

    // 绘制橡皮线
    if(!(temppt.isNull() || lastpt.isNull()) && !isFirst)
    {
        QPen pen;
        pen.setWidth(0);
        pen.setColor(QColor(255,0,0));
        pen.setStyle(Qt::DashLine);
        painter->setPen(pen);

        if(mCurDrawType == DLine)
            painter->drawLine(temppt, lastpt);
        else if(mCurDrawType == DCircle)
        {
            QPointF center = temppt;
            qreal rad = QLineF(temppt, lastpt).length();
            painter->drawEllipse(center.x() - rad, center.y() - rad, rad * 2, rad * 2);
            painter->drawLine(temppt, lastpt);
        }
    }

    // 绘制已添加的图元
    QPen pen;
    pen.setCosmetic(true);
    painter->setPen(pen);
    painter->setWorldTransform(m_trans, true);

    foreach (auto i, m_mainlist) {
        i->Draw(painter);
    }

    pen.setCosmetic(false);
    painter->setPen(pen);
    painter->restore();

    delete painter;
}

void MainWindow::on_actionPoint_triggered()
{
    mCurModifyType = MNone;
    mCurDrawType = DPoint;
    this->setCursor(Qt::CrossCursor);
}


void MainWindow::on_actionLine_triggered()
{
    mCurModifyType = MNone;
    mCurDrawType = DLine;
    this->setCursor(Qt::CrossCursor);
}


void MainWindow::on_actionCircle_triggered()
{
    mCurModifyType = MNone;
    mCurDrawType = DCircle;
    this->setCursor(Qt::CrossCursor);
}


void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        if(mCurDrawType == DLine)
        {
            if(isFirst)
            {
                temppt = event->pos();
                isFirst = false;
            }
            else
            {
                DwgLine *line = new DwgLine;            // 创建并初始化一个直线对象
                QPointF pt1 = m_trans.inverted().map(temppt);
                QPointF pt2 = m_trans.inverted().map(event->pos());
                line->SetData(pt1,pt2);		// 使用SetData函数设置私有成员变量
                m_mainlist.append(line);		// append函数用于把对象添加到数组mLines中
                update();						// 该函数用于刷新窗口
                isFirst = true;
            }
        }
        if(mCurDrawType == DCircle)
        {
            if(isFirst)
            {
                temppt = event->pos();
                isFirst = false;
            }
            else
            {
                DwgCircle *circle = new DwgCircle;            // 创建并初始化一个直线对象

                QPointF pt1 = m_trans.inverted().map(temppt);
                QPointF pt2 = m_trans.inverted().map(event->pos());
                circle->SetData(pt1,pt2);		// 使用SetData函数设置私有成员变量
                m_mainlist.append(circle);        // append函数用于把对象添加到数组mLines中
                update();                                   // 该函数用于刷新窗口
                isFirst = true;
            }
        }
        if(mCurDrawType == DPoint)
        {
            DwgPoint *point = new DwgPoint; // 创建一个点对象
            QPointF pt = m_trans.inverted().map(event->pos());
            point->SetData(pt, pt); // 设置点的位置
            m_mainlist.append(point); // 将点添加到图元列表
            update(); // 更新视图
        }
        if(mCurModifyType == MSelect)
        {
            QPointF spt = m_trans.inverted().map(event->pos());
            foreach (auto obj, m_mainlist)
            {
                obj->mSelected = false;
            }
            foreach (auto i, m_mainlist) {
                i->Select(spt);
            }
            update();
        }
    }

}

void MainWindow::on_actionNew_triggered()
{
    qDeleteAll(m_mainlist);
    m_mainlist.clear();
    update();
    ui->statusbar->showMessage("开始绘制一张新的图纸：",3000);
}


void MainWindow::on_actionExit_triggered()
{
    qDeleteAll(m_mainlist);
    m_mainlist.clear();
    this->close();
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    m_winScale = m_winScale +(event->angleDelta().y()/120.0/10);

    // 下面的代码用于计算m_trans
    m_trans.reset();
    m_trans.translate(0,this->height());
    m_trans.scale(1,-1);
    m_trans.scale(m_winScale,m_winScale);

    qDebug() <<  m_trans;
    update();
}



void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(!movept.isNull())
    {
        if(event->buttons() & Qt::MiddleButton)
            m_trans.translate(event->pos().x() - movept.x(),
                              movept.y() - event->pos().y());
    }
    movept = event->pos();

    if(!isFirst)
    {
        lastpt = event->pos();
        update(); // 确保触发重绘
    }
    QMainWindow::mouseMoveEvent(event);
}

void MainWindow::on_actionSelect_triggered()
{
    // 设置当前修改类型为选择模式
    mCurModifyType = MSelect;
    // 设置当前绘制类型为无
    mCurDrawType = DNone;
    // 改变光标形状为全方向调整大小光标
    this->setCursor(Qt::SizeAllCursor);
    // 更新视图，确保清除选中状态后正确显示
    update();
}


void MainWindow::on_actionDelete_triggered()
{
    this->setCursor(Qt::ArrowCursor);
    foreach(auto i , m_mainlist)
    {
        if(i->mSelected)
            i->mDeleteed = true;
    }
    update();
}

